﻿using System.Drawing;
using System.Text;
using Hangfire;
using Microsoft.AspNetCore.Mvc;
using Net7Docker.Common;
using Newtonsoft.Json;
using SkiaSharp;

namespace Net7Docker.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HealthController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<HealthController> _logger;
        private readonly IWebHostEnvironment _webHostEnvironment;

        public HealthController(ILogger<HealthController> logger, IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
        {
            _logger = logger;
            _configuration = configuration;
            _webHostEnvironment = webHostEnvironment;
        }

        [HttpGet]
        [Route("test")]
        public string Test()
        {
            string enname = _webHostEnvironment.EnvironmentName;

            _logger.LogTrace("trace");
            _logger.LogDebug("debug");

            _logger.LogInformation($"进来方法了,当前环境名为:{enname}");

            var aa = _configuration["app:test"];

            _logger.LogInformation($"aa的值:{aa}");

            try
            {
                int a = 0;
                int b = 10;
                int c = b / a;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "发生错误");
                _logger.LogCritical(ex, "致命错误");

            }
            _logger.LogWarning("警告");



            return $"{enname}:{aa}";
        }

        [HttpGet]
        [Route("image")]
        public string Image222()
        {
            // 创建一个位图对象，指定宽度和高度
            using var bitmap = new SKBitmap(200, 200);
            // 创建一个绘图上下文
            using var canvas = new SKCanvas(bitmap);
            // 设置背景颜色
            canvas.Clear(SKColors.White);

            //设置字体为微软雅黑
            var fontManager = SKFontManager.Default;
            _logger.LogInformation($"fontManager为:{JsonConvert.SerializeObject(fontManager)}");

            var typeface = fontManager.MatchFamily("Microsoft YaHei UI");
            _logger.LogInformation($"typeface:{JsonConvert.SerializeObject(typeface)}");

            using var paint = new SKPaint
            {
                Color = SKColors.Red,
                TextSize = 24,
                IsAntialias = true,
                TextAlign = SKTextAlign.Center,
                Typeface = typeface,
            };

            // 绘制文本
            canvas.DrawText("赵宝宝!", bitmap.Width / 2, bitmap.Height / 2, paint);

            // 保存图片到文件或内存流中
            var path = Path.Combine(_webHostEnvironment.ContentRootPath, "Images");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            var filepath = Path.Combine(path, $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg");

            using var image = SKImage.FromBitmap(bitmap);
            using var data = image.Encode(SKEncodedImageFormat.Jpeg, 100);
            using var stream = new FileStream(filepath, FileMode.Create);
            data.SaveTo(stream);

            return "生成图片成功11122起飞了起飞了";
        }

        [HttpGet]
        [Route("hangfiretest")]
        public string HangfireTest()
        {
            //不支持秒级，比较垃圾
            //BackgroundJob.Schedule(() => Console.WriteLine($"{DateTime.Now}==>Hello, world   2222"), TimeSpan.FromSeconds(5));

            //每分钟执行一次
            //RecurringJob.AddOrUpdate("easyjob", () => Console.Write("Easy!"), Cron.Minutely);；
            //RecurringJob.AddOrUpdate("fcjob", () => aa(), "0/10 * * * * ?");


            return "设置作业完成";
        }

        [NonAction]
        public void aa()
        {
            _logger.LogInformation("sb11111");
        }

        [HttpGet]
        [Route("compare")]
        public string Compare()
        {
            var student1 = "雷电法王";
            var student2 = "雷电法王";
            var student3 = "雷电" + "法王";
            var part = "法王";
            var student31 = "雷电" + part;
            var student4 = string.Format("雷电{0}", "法王");
            var sb = new StringBuilder();
            sb.Append("雷电");
            sb.Append("法王");
            var student5 = sb.ToString();
            var student6 = $"{"雷电"}{"法王"}";
            _logger.LogInformation("s1==>s2" + object.ReferenceEquals(student1, student2));
            _logger.LogInformation("s1==>s3" + object.ReferenceEquals(student1, student3));
            _logger.LogInformation("s1==>s31" + object.ReferenceEquals(student1, student31));
            _logger.LogInformation("s1==>s4" + object.ReferenceEquals(student1, student4));
            _logger.LogInformation("s1==>s5" + object.ReferenceEquals(student1, student5));
            _logger.LogInformation("s1==>s6" + object.ReferenceEquals(student1, student6));
            return "比较成功";
        }

        [HttpGet]
        [Route("GetDataById")]
        public string GetDataById(int id)
        {
            SqlHelper sqlHelper = new SqlHelper();
            var company = sqlHelper.Find<Company>(id);
            return JsonConvert.SerializeObject(company);
        }

        [HttpGet]
        [Route("SystemDrawCommon")]
        public string SystemDrawCommon()
        {
            Bitmap bitmap = new Bitmap(200, 200); // 创建一个200x200像素的位图  
            Graphics graphics = Graphics.FromImage(bitmap); // 从位图创建Graphics对象  

            // 使用Graphics对象绘制形状、文本等。例如：  
            graphics.DrawEllipse(Pens.Red, 50, 50, 100, 100); // 绘制一个红色的圆  
            graphics.DrawLine(Pens.Black, 50, 50, 150, 150); // 绘制一条线段  
            graphics.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, new PointF(50, 50)); // 在位图上绘制文本  

            // 最后，不要忘记释放Graphics资源  
            graphics.Dispose();
            bitmap.Save($"{DateTime.Now:yyyyMMddHHmmss}_output.png"); // 将位图保存为PNG文件，也可以保存为其他格式  
            bitmap.Dispose(); // 释放位图资源
            return "生成成功";
        }

    }
}
